package cn.zmwh.im.server.modules.message.service;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import cn.zmwh.im.common.pojo.dto.IMUserInfo;
import cn.zmwh.im.common.pojo.dto.transfer.Message;
import cn.zmwh.im.server.config.HintManager;
import cn.zmwh.im.server.constant.BsKImConstant;
import cn.zmwh.im.server.modules.message.mapper.ImGroupMessageMapper;
import cn.zmwh.im.server.modules.message.pojo.entity.ImGroupMessage;
import cn.zmwh.im.server.modules.message.pojo.request.GroupHistory;
import cn.zmwh.im.server.modules.message.pojo.request.HistoryRequest;
import cn.zmwh.im.server.modules.ums.pojo.entity.ImGroup;
import cn.zmwh.im.server.modules.ums.pojo.entity.ImGroupMember;
import cn.zmwh.im.server.modules.ums.service.ImGroupMemberService;
import cn.zmwh.im.server.modules.ums.service.ImGroupService;
import cn.zmwh.im.server.netty.ReissueMessageService;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 群消息 服务实现类
 * </p>
 *
 * @author dmzmwh
 * @since 2025-06-19
 */
@Slf4j
@Service
public class ImGroupMessageService extends ServiceImpl<ImGroupMessageMapper, ImGroupMessage> {

    @Autowired
    private ImGroupMemberService imGroupMemberService;
    @Autowired
    private ImGroupService imGroupService;
    @Autowired
    private ReissueMessageService reissueMessageService;

    public void history(HistoryRequest request, IMUserInfo receiver) {
        Long uid = request.getUid();
        List<GroupHistory> groupList = request.getGroupList();
        if (CollUtil.isNotEmpty(groupList)) {
            return;
        }
        List<ImGroupMessage> listAll = new ArrayList<>();
        try (HintManager hintManager = HintManager.getInstance()) {
            for (GroupHistory item : groupList) {
                Long groupId = item.getGroupId();
                ImGroupMember imGroupMember = imGroupMemberService.getOne(Wrappers.<ImGroupMember>lambdaQuery()
                        .eq(ImGroupMember::getGroupId, groupId)
                        .eq(ImGroupMember::getUid, uid));
                if (imGroupMember == null) {
                    log.error("用户:{}未加入群组:{}则不推送消息", uid, groupId);
                    continue;
                }
                ImGroup byId = imGroupService.getById(groupId);
                Integer messageDb = byId.getMessageDb();
                Integer messageTable = byId.getMessageTable();
                hintManager.addDbValue(BsKImConstant.DbName.IM_MESSAGE, messageDb);
                hintManager.addTableValue(messageTable);
                String messageId = item.getMessageId();
                Date historyTime = DateUtil.offsetMonth(new Date(), -2);
                if (StrUtil.isBlank(messageId)){
                    ImGroupMessage one = getOne(Wrappers.<ImGroupMessage>lambdaQuery()
                            .eq(ImGroupMessage::getGroupId, groupId)
                            .eq(ImGroupMessage::getMessageId, messageId));
                    if (one != null && one.getSendTime().after(historyTime)) {
                        historyTime = one.getSendTime();
                    }
                }
                List<ImGroupMessage> list = list(Wrappers.<ImGroupMessage>lambdaQuery()
                        .eq(ImGroupMessage::getGroupId, groupId)
                        .gt(ImGroupMessage::getSendTime, historyTime)
                        .orderByAsc(Message::getSendTime));
                listAll.addAll(list);
            }
        }
        reissueMessageService.messageSendGroupHistory(listAll, receiver);
    }
}
